package com.newlink.building.userinfo.activity

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.TextView
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityAccountInformationChangePhoneBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_AccountInformationChangePhonePresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_AccountInformationChangePhoneContract

/**
 * @author wangyang
 * @date 2022/10/28
 * @description 我的——账户信息——更换手机号页面（第一步：输入密码）
 */
class Module_UserInfo_AccountInformationChangePhoneActivityNL :
    NL_BaseMvpActivity<Module_UserInfo_AccountInformationChangePhoneContract.View, Module_UserInfo_AccountInformationChangePhoneContract.Presenter>(),
    Module_UserInfo_AccountInformationChangePhoneContract.View, View.OnClickListener {

    private var isShowPassword = false
    private var mPhoneNumber: String = ""

//    override fun attachLayoutRes(): Int = R.layout.activity_account_information_change_phone

    private lateinit var mBinding: ActivityAccountInformationChangePhoneBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityAccountInformationChangePhoneBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
    }

    override fun initView() {
        findViewById<TextView>(R.id.tv_title_bar_center).text =
            resources.getText(R.string.account_information_change_phone_title)

        val content = getString(R.string.account_information_change_phone_describe, phoneNumber)

        mBinding.tvDescribe.text = content

        mBinding.ivShowPassword.setOnClickListener(this)
        mBinding.btnNext.setOnClickListener(this)

        //密码的输入框监听
        mBinding.etPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                checkChangeButtonState()
            }

        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_show_password -> {
                //显示/隐藏密码
                isShowPassword = !isShowPassword
                mBinding.etPassword.transformationMethod =
                    if (isShowPassword) HideReturnsTransformationMethod.getInstance() else PasswordTransformationMethod.getInstance()

                if (isShowPassword) {
                    mBinding.ivShowPassword.setImageResource(R.mipmap.password_hidden)
                } else {
                    mBinding.ivShowPassword.setImageResource(R.mipmap.password_show)
                }
                mBinding.etPassword.setSelection(mBinding.etPassword.text.length)
            }

            R.id.btn_next -> {
                if (mBinding.etPassword.text.toString().isEmpty()) {
                    showToast("输入密码不能为空")
                    return
                }
                mPresenter?.verifyPassword(accessToken, mBinding.etPassword.text.toString())
            }
        }
    }

    /**
     * 下一步按钮的状态
     */
    fun checkChangeButtonState() {
        val inputPassword = mBinding.etPassword.text.trim().toString()

        mBinding.btnNext.isEnabled = inputPassword.isNotEmpty()
    }

    override fun createPresenter(): Module_UserInfo_AccountInformationChangePhoneContract.Presenter =
        Module_UserInfo_AccountInformationChangePhonePresenter()

    override fun showData() {
    }

    override fun verifyPasswordSuccess() {
        //下一步
        startActivity(
            Intent(
                this,
                Module_UserInfo_AccountInformationChangePhoneNewPhoneActivityNL::class.java
            )
        )
    }

    override fun verifyPasswordFailed(msg: String) {
        showToast(msg)
    }
}